<?php
use Illuminate\Database\Eloquent\SoftDeletingTrait;

class Size extends Eloquent {
	
	use SoftDeletingTrait;
	
	protected $dates = ['deleted_at'];

	protected $table = 'size';

	/**
	 * Condition: Unarchived Items.
	 * @author AngelaMisa 02/22/2015 2:25AM
	 * @param $query
	 * @return mixed
	 */
	public function scopeActive( $query ) {
		return $query->where($this->table . '.archived', 0);
	}

	/**
	 * Condition: Unarchived Ordered Items with given status.
	 * @author AngelaMisa 02/22/2015 2:25AM
	 * @param $query, $status, $company
	 * @return mixed
	 */
	public function scopeHasOrders( $query, $status = null, $company = null ) {
		return $query->where($this->table . '.archived', 0)
			->whereHas('orderProducts', function( $q ) use ( $status, $company ) {
				$q->where('archived', '0');
				
				if ( !is_null($company) ) {
					$q->whereHas('order', function( $query ) use ( $status, $company ) {
						$query->where('COMPANY_id', $company);
					});
				}
				
				if ( !is_null($status) ) $q->where( 'status', $status );
			});
	}

	/**
	 * Retrieves Ordered Items with Size.
	 * @author AngelaMisa 02/22/2015 2:25AM
	 * @return mixed
	 */
	public function orderProducts() {
		return $this->hasMany('OrderProduct', 'SIZE_id');
	}

	/**
	 * Retrieves Products with Size.
	 * @author AngelaMisa 02/22/2015 2:25AM
	 * @return mixed
	 */
	public function products() {
		return $this->hasMany('Product', 'SIZE_id');
	}
}